<template>
  <a-spin :spinning="confirmLoading">
    <tsl-step :list="steplist" :currentStatus="null"></tsl-step>

    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24" >
            <a-form-model-item label="单据编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="code">
              <a-input v-model="model.code" placeholder="请输入单据编号" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="单据类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
<!--              <a-input v-model="model.type" placeholder="请输入单据类型" ></a-input>-->
              <j-dict-select-tag  placeholder="请选择状态" v-model="model.type" dictCode="rk_type"  @change='handlerChanged' />
            </a-form-model-item>
          </a-col >
          <a-col :span="24" v-show='model.type && (model.type==0 || model.type==1)'>
            <a-form-model-item label="到货类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <j-dict-select-tag  placeholder="请选择状态" v-model="model.arrayType" dictCode="arrival_type"  @change='handlerChanged' />
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="24" >-->
<!--            <a-form-model-item label="批次号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lotNumber">-->
<!--              <a-input v-model="model.lotNumber" placeholder="请输入批次号" ></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :span="24" >-->
<!--            <a-form-model-item label="进场日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="factoryTime">-->
<!--              <j-date placeholder="请选择进场日期" v-model="model.factoryTime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
          <a-col :span="24" >
            <a-form-model-item label="采购编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="procurementId">
              <a-input v-model="model.procurementId" placeholder="请输入采购id" v-show='false'></a-input>
              <a-input-search
                v-model="model.procurementCode"
                placeholder="请选择采购编号"
                style="width: 200px"
                @search="onSearchProcurement"
                :disabled='showProcurement'
              />
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="12" >-->
<!--            <a-form-model-item label="供应商" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="procurementId">-->
<!--              <a-input v-model="model.supplierName" placeholder="请输入供应商" disabled></a-input>-->
<!--              <a-input v-model="model.supplierId" placeholder="请输入供应商"  v-show='false'></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
          <a-col :span="24" >
            <a-form-model-item label="供应商" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="venderId">
              <a-input v-model="model.supplierId" placeholder="请输入供应商" v-show='false'></a-input>
              <a-input-search
                v-model="model.supplierName"
                placeholder="请选择供应商"
                style="width: 200px"
                @search="onSearchSupplier"
                :disabled='showSupplier'
              />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
    <a-tabs
            :default-active-key="tabkeys[0]"
            >
      <a-tab-pane tab="入库信息" :key="tabkeys[0]"  :forceRender="true">
        <a-card title="收货信息" :bordered='false'>
          <j-vxe-table
             keep-source
            :ref="refKeys[0]"
            :loading="bussStorageNoticeMaterialTable.loading"
            :columns="bussStorageNoticeMaterialTable.columns"
            :dataSource="bussStorageNoticeMaterialTable.dataSource"
            :maxHeight="300"
            :disabled="formDisabled"
            :rowNumber="true"
            :rowSelection="true"
            :toolbar="true"
            :toolbarConfig="{slot: ['prefix', 'suffix'], btn: ['clearSelection']}"

          >
            <template    v-slot:toolbarSuffix>
              <a-button @click='addMaterial' v-if='!procurementShow && model.type'>新增</a-button>
              <a-button @click='addMaterial' >新增</a-button>
              <a-button @click='deleteMaterial' >删除</a-button>
              <a-button @click='addMaterial' >添加车辆信息</a-button>
              <a-button @click='clearCheck' >清空选择</a-button>
<!--              <a-button @click='addMaterial' v-if='!procurementShow && model.type'>新增物料</a-button>-->
            </template>
<!--            <template v-slot:action="props">-->
<!--              <a-space>-->
<!--                <a @click="handleEdit(props)">编辑</a>-->
<!--                <a-divider type="vertical"/>-->
<!--                <a @click="handleDeleteMaterical(props)">删除</a>-->
<!--                <a-divider type="vertical"/>-->
<!--                <a @click="handleCK(props)">添加车辆信息</a>-->
<!--              </a-space>-->
<!--              &lt;!&ndash;          <a-popconfirm title="确定删除吗？" @confirm="handleDL(props)">&ndash;&gt;-->
<!--              &lt;!&ndash;            <a>删除</a>&ndash;&gt;-->
<!--              &lt;!&ndash;          </a-popconfirm>&ndash;&gt;-->
<!--            </template>-->
          </j-vxe-table>
        </a-card>
        <a-card title="车辆信息" :bordered='false'>
          <j-vxe-table
            keep-source
            :ref="refKeys[1]"
            :loading="bussStorageNoticeCarinfoTable.loading"
            :columns="bussStorageNoticeCarinfoTable.columns"
            :dataSource="bussStorageNoticeCarinfoTable.dataSource"
            :maxHeight="300"
            :disabled="formDisabled"
            :rowNumber="true"
            :rowSelection="true"
            :toolbar="true"
            :autoResize="true"
            :toolbarConfig="{slot: ['prefix', 'suffix'], btn: ['clearSelection']}"
          >
            <template v-slot:action="props">
              <a-space>
                <a @click="handleEditCar(props)">编辑</a>
                <a-divider type="vertical"/>
                <a @click="deleteCarinfo(props)">删除</a>
                <a-divider type="vertical"/>
              </a-space>
              <!--          <a-popconfirm title="确定删除吗？" @confirm="handleDL(props)">-->
              <!--            <a>删除</a>-->
              <!--          </a-popconfirm>-->
            </template>
          </j-vxe-table>
        </a-card>
      </a-tab-pane>
      <a-tab-pane tab="状态履历修改" :key="tabkeys[1]"  :forceRender="true">
        <a-card :bordered='false'>
          <a-timeline>
            <a-timeline-item color="green">Create a services site 2015-09-01</a-timeline-item>
            <a-timeline-item color="green">Create a services site 2015-09-01</a-timeline-item>
            <a-timeline-item color="red">
              <p>Solve initial network problems 1</p>
              <p>Solve initial network problems 2</p>
              <p>Solve initial network problems 3 2015-09-01</p>
            </a-timeline-item>
            <a-timeline-item>
              <p>Technical testing 1</p>
              <p>Technical testing 2</p>
              <p>Technical testing 3 2015-09-01</p>
            </a-timeline-item>
            <a-timeline-item color="gray">
              <p>Technical testing 1</p>
              <p>Technical testing 2</p>
              <p>Technical testing 3 2015-09-01</p>
            </a-timeline-item>
            <a-timeline-item color="gray">
              <p>Technical testing 1</p>
              <p>Technical testing 2</p>
              <p>Technical testing 3 2015-09-01</p>
            </a-timeline-item>
          </a-timeline>
        </a-card>
      </a-tab-pane>
    </a-tabs>

    <!-- 采购单 -->
    <buss-material-procurement-list
                ref='procurement' @handleOk='procurementOk' :procurementType='procurementType'></buss-material-procurement-list>

    <erp-supplier-list ref='supplier' @handlerOk='supplierOk'> </erp-supplier-list>

    <erp-good-list ref='good'
                   @handlerOk='goodOk'
                   checkval='checkbox'
                    :supplierId="model.supplierId"></erp-good-list>


    <erp-location-list ref="location" @handlerOk="locationOk"></erp-location-list>

    <details-modal ref="details" @detailsConfirm="handleDetailsConfirm">
      <template v-slot:mainForm="{row}">
        <template v-if="row">
          <a-form-model
            ref="mainForm"
            :model="row"
            :rules="rules"
            :label-col="labelCol"
            :wrapper-col="wrapperCol"
          >
            <a-row :gutter="8">
              <a-col :span="8">
                <a-form-model-item label="ID" prop="id">
                  <a-input v-model="row.id" disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="物料编号" prop="materialCode">
                  <a-input v-model="row.materialCode" disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="物料名称" prop="materialName">
                  <a-input v-model="row.materialName" disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="规格" prop="spec" >
                  <a-input v-model="row.spec" disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="单位" prop="meUnit">
                  <j-dict-select-tag  v-model="row.meUnit" dictCode="ji_unit"   disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="采购订单编号" prop="procurementCode">
                  <a-input v-model="row.procurementCode" disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="排产单号" prop="schedulingCode">
                  <a-input v-model="row.schedulingCode" />
                </a-form-model-item>
              </a-col>
              <a-col :span="8" v-show='false'>
                <a-form-model-item label="原料id" prop="materialId">
                  <a-input v-model="row.materialId" />
                </a-form-model-item>
              </a-col>
              <a-col :span="8" >
                <a-form-model-item label="采购数量" prop="procurementNum">
                  <a-input v-model="row.procurementNum" disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8" >
                <a-form-model-item label="辅助数量" prop="supportNum">
                  <a-input v-model="row.supportNum" />
                </a-form-model-item>
              </a-col>
              <a-col :span="8" >
                <a-form-model-item label="辅助单位" prop="supportNum">
                  <a-input v-model="row.supportUnit" />
                </a-form-model-item>
              </a-col>
              <a-col :span="8" >
                <a-form-model-item label="已到货数量-未作" prop="supportNum">
                  <a-input v-model="row.id" />
                </a-form-model-item>
              </a-col>
              <a-col :span="8" >
                <a-form-model-item label="仓库编号" prop="supportNum">
                  <a-input v-model="row.erpWarehouseCode" disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8" >
                <a-form-model-item label="仓库名称" prop="supportNum">
                  <a-input v-model="row.erpWarehouseName" disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8" >
                <a-form-model-item label="库位编码" prop="supportNum">
                  <a-input-search
                    v-model="row.erpLocationCode"
                    placeholder="请选择库位"
                    style="width: 200px"
                    @search="onSelectLocation"
                  />
                  <!--   <a-input v-model="row.erpLocationCode" disabled/>-->

                  <!--       <JSearchSelectTag-->
                  <!--             placeholder="请选择供应商"-->
                  <!--                      v-model="model.supplierName"-->
                  <!--                  @change='handlerSelect'-->
                  <!--                      dict="erp_supplier,name,id"-->
                  <!--                      :disabled='disableSelect'-->
                  <!--                    />-->
                </a-form-model-item>
              </a-col>
              <a-col :span="8" >
                <a-form-model-item label="库位名称" prop="supportNum">
                  <a-input v-model="row.erpLocationName" disabled/>
                  <a-input v-model="row.erpLocationId" v-show='false'/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8" >
                <a-form-model-item label="批次号" prop="procurementNum">
                  <a-input v-model="row.batchNum" />
                </a-form-model-item>
              </a-col>
              <a-col :span="8" >
                <a-form-model-item label="物料批号" prop="materialLotNum">
                  <a-input v-model="row.materialLotNum" />
                </a-form-model-item>
              </a-col>
              <a-col :span="8" >
                <a-form-model-item label="原发数量" prop="originalNum">
                  <a-input-number v-model="row.originalNum" :min="0"   />
                </a-form-model-item>
              </a-col>
              <a-col :span="8" >
                <a-form-model-item label="实收数量" prop="actualNum">
                  <a-input-number v-model="row.actualNum" :min="0"   />
                </a-form-model-item>
              </a-col>
              <a-col :span="8" >
                <a-form-model-item label="入库日期" prop="warehouseDate">
                  <!--                    <a-date-picker v-model="row.warehouseDate" />-->
                  <j-date v-model='row.warehouseDate'></j-date>
                </a-form-model-item>
              </a-col>
              <a-col :span="8" >
                <a-form-model-item label="生产日期" prop="createDate">
                  <j-date v-model='row.createDate'></j-date>
                  <!--                    <a-date-picker v-model="row.createDate" format="YYYY-MM-DD"/>-->
                </a-form-model-item>
              </a-col>
              <a-col :span="8" >
                <a-form-model-item label="限用日期" prop="expiryDate">
                  <j-date v-model='row.expiryDate'></j-date>
                  <!--                    <a-date-picker v-model="row.expiryDate" />-->
                </a-form-model-item>
              </a-col>
              <a-col :span="8" >
                <a-form-model-item label="备注" prop="remark">
                  <a-textarea v-model="row.remark" showCount :maxlength="100" />
                </a-form-model-item>
              </a-col>
              <a-col :span="8" v-show="false">
                <a-form-model-item label="当前状态" prop="status">
                  <a-input v-model="row.status" />
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-form-model>
        </template>

      </template>


    </details-modal>
    <details-modal ref="carDetail" @detailsConfirm="handleDetailsConfirm">
      <template v-slot:mainForm="{row}">
        <template v-if="row">
          <a-form-model
            ref="subForm"
            :model="row"
            :rules="rules"
            :label-col="labelCol"
            :wrapper-col="wrapperCol"
          >
            <a-row :gutter="8">
              <a-col :span="8" v-show="false">
                <a-form-model-item label="ID" prop="id">
                  <a-input v-model="row.id" disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8" v-show="false">
                <a-form-model-item label="ID" prop="id">
                  <a-input v-model="row.id" disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="车牌号" prop="carNum">
                  <a-input v-model="row.carNum" />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="物料编号" prop="materialCode">
                  <a-input v-model="row.materialCode" disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="物料名称" prop="materialName">
                  <a-input v-model="row.materialName" disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="单位" prop="meUnit">
                  <j-dict-select-tag   v-model="row.meUnit" dictCode="ji_unit"   disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="原发数" prop="originalNum">
                  <a-input v-model="row.originalNum" />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="实际数量" prop="actualNum">
                  <a-input v-model="row.actualNum" />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="备注" prop="remark">
                  <a-input v-model="row.remark" />
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-form-model>
        </template>
      </template>
    </details-modal>
  </a-spin>
</template>

<script>
  import { getAction } from '@/api/manage'
  import { JVxeTableModelMixin } from '@/mixins/JVxeTableModelMixin.js'
  import { JVXETypes } from '@/components/jeecg/JVxeTable'
  import { getRefPromise,VALIDATE_FAILED} from '@/components/jeecg/JVxeTable/utils/vxeUtils.js'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import BussMaterialProcurementList  from '@/views/modules/procurement/BussMaterialProcurementList'
  import ErpSupplierList from '@/views/modules/supplier/ErpSupplierList'
  import ErpGoodList from '@/views/modules/goods/ErpGoodsListModal'
  import ErpLocationList from '@/views/modules/location/ErpLocationList'
  import DetailsModal from '@/components/modal/DetailsModal'
  import TslStep from '@/components/tsl/TslStep'

  export default {
    name: 'BussStorageNoticeForm',
    mixins: [JVxeTableModelMixin],
    components: {
      JFormContainer,BussMaterialProcurementList,ErpSupplierList,ErpGoodList,ErpLocationList,
      DetailsModal,TslStep
    },
    data() {
      const actualNum = (rule, value, callback) => {
        callback(new Error('请再次输入密码'));
        // if (value === '') {
        //   callback(new Error('请再次输入密码'));
        // } else if (value !== this.formInfo.password) {
        //   callback(new Error('两次密码不同'));
        // } else {
        //   callback();
        // }
      };
      return {
        toolBtn: [ 'remove', 'clearSelection'],
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        model:{
         },
        steplist:[
          {title:'草稿'},{title:'采购确认'},{title:'入库'},{title:'状态之间转换'}
        ],
        rules: {
          num: [
            {required: true, message: '必须输入序号'},
          ],
          actualNum: [
            // { required: true, trigger: "blur", validator: actualNum },
          ]
        },
        noticeMaterialRowNum: 0,
        carRowNum: 0,
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 0,
        validatorRules: {
        },
        refKeys: ['bussStorageNoticeMaterial', 'bussStorageNoticeCarinfo', ],
        tableKeys:['bussStorageNoticeMaterial', 'bussStorageNoticeCarinfo', ],
        tabkeys: ['material','uphistorys'],
        activeKey: 'material',
        // 入库通知单原料单
        bussStorageNoticeMaterialTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '通知id',
              key: 'noticeId',
              type: JVXETypes.hidden,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '物料编号',
              key: 'materialCode',
              type: JVXETypes.normal,
              width: '200px',
            },
            {
              title: '物料名称',
              key: 'materialName',
              type: JVXETypes.normal,
              width: '200px',
            },
            {
              title: '规格',
              key: 'spec',
              type: JVXETypes.normal,
              width: '200px',
            },
            {
              title: '单位',
              key: 'meUnit',
              type: JVXETypes.select,
              width: '200px',
              dictCode: 'ji_unit',
              disabled: true
            },
            {
              title: '采购订单编号',
              key: 'procurementCode',
              type: JVXETypes.normal,
              width: '200px',
            },
            {
              title: '排产单号',
              key: 'schedulingCode',
              type: JVXETypes.normal,
              width: '200px',
            },
            {
              title: '采购明细id',
              key: 'procurementDetailId',
              type: JVXETypes.hidden,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '原料id',
              key: 'materialId',
              type: JVXETypes.normal,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '采购数量',
              key: 'procurementNum',
              type: JVXETypes.normal,
              width: '200px',
            },
            {
              title: '辅助数量',
              key: 'supportNum',
              type: JVXETypes.normal,
              width: '200px',
            },
            {
              title: '辅助单位',
              key: 'supportUnit',
              type: JVXETypes.normal,
              width: '200px',
            },
            {
              title: '已到货数量-未作',
              key: 'procurementNum',
              type: JVXETypes.normal,
              width: '200px',
            },
            {
              title: '仓库编号',
              key: 'erpWarehouseCode',
              type: JVXETypes.normal,
              width: '200px',
            },
            {
              title: '仓库名称',
              key: 'erpWarehouseName',
              type: JVXETypes.normal,
              width: '200px',
            },
            {
              title: '库位编码',
              key: 'erpLocationCode',
              type: JVXETypes.normal,
              width: '200px',
            },
            {
              title: '库位名称',
              key: 'erpLocationName',
              type: JVXETypes.normal,
              width: '200px',
            },
            {
              title: '库位id',
              key: 'erpLocationId',
              type: JVXETypes.hidden,
              width: '200px',
            },
            {
              title: '库位id',
              key: 'erpLocationId',
              type: JVXETypes.hidden,
              width: '200px',
            },
            {
              title: '批次号',
              key: 'batchNum',
              type: JVXETypes.normal,
              width: '200px',
            },
            {
              title: '物料批号',
              key: 'materialLotNum',
              type: JVXETypes.normal,
              width: '200px',
            },
            {
              title: '原发数量',
              key: 'originalNum',
              type: JVXETypes.normal,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '实收数量',
              key: 'actualNum',
              type: JVXETypes.normal,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '入库日期',
              key: 'warehouseDate',
              type: JVXETypes.normal,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '生产日期',
              key: 'createDate',
              type: JVXETypes.normal,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '限用日期',
              key: 'expiryDate',
              type: JVXETypes.normal,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '当前状态',
              key: 'status',
              type: JVXETypes.normal,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            // {
            //   title: '库位',
            //   key: 'storehouse',
            //   type: JVXETypes.input,
            //   width:"200px",
            //   placeholder: '请输入${title}',
            //   defaultValue:'',
            // },
            {
              title: '备注',
              key: 'remark',
              type: JVXETypes.normal,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '操作',
              key: 'action',
              type: JVXETypes.slot,
              fixed: 'right',
              minWidth: '200px',
              align: 'center',
              slotName: 'action',
            }
          ]
        },
        // 入库通知单-车辆信息
        bussStorageNoticeCarinfoTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: 'id',
              key: 'id',
              type: JVXETypes.hidden,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '通知单id',
              key: 'noticeId',
              type: JVXETypes.hidden,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '车牌号',
              key: 'carNum',
               type: JVXETypes.normal,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '物料编号',
              key: 'materialCode',
              type: JVXETypes.normal,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '物料名称',
              key: 'materialName',
              type: JVXETypes.normal,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '单位',
              key: 'meUnit',
              type: JVXETypes.select,
              width: '200px',
              dictCode: 'ji_unit',
              disabled: true
            },
            {
              title: '原发数',
              key: 'originalNum',
               type: JVXETypes.normal,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '实际数量',
              key: 'actualNum',
               type: JVXETypes.normal,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '备注',
              key: 'remark',
               type: JVXETypes.normal,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '操作',
              key: 'action',
              type: JVXETypes.slot,
              fixed: 'right',
              minWidth: '200px',
              align: 'center',
              slotName: 'action',
            }
          ]
        },
        NoticeMaterialTable: '',
        CarTableRef: '',
        url: {
          add: "/business/bussStorageNotice/add",
          edit: "/business/bussStorageNotice/edit",
          queryById: "/business/bussStorageNotice/queryById",
          bussStorageNoticeCarinfo: {
            list: '/business/bussStorageNotice/queryBussStorageNoticeCarinfoByMainId'
          },
          bussStorageNoticeMaterial: {
            list: '/business/bussStorageNotice/queryBussStorageNoticeMaterialByMainId'
          },
        }
      }
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
      procurementType(){
        return this.model.type | 0
      },
      procurementShow: {
        get(){
          if(typeof this.model.type === 'undefined'){
            return false
          }
          if((this.model.type == 0 || this.model.type == 1) && (this.model.arrayType)){
            return false
          }
          return this.model.type == 0 || this.model.type == 1
        },
        set(){}
      },
      showProcurement() {
        if(this.model.type && (this.model.type == 0 || this.model.type == 1) && (!this.model.arrayType || this.model.arrayType == '')){
          return false;
        }
        return true;
      },
      showSupplier(){
        if(this.model.type && this.model.type == 2){
          return false;
        }
        return true;
      }
    },
    created () {

    },
    mounted() {
      this.$nextTick(() => {
        this.getAllTable().then(tables=>{
          this.NoticeMaterialTable = tables[0]
          this.CarTableRef = tables[1]
        })
        setTimeout(() => {
          if(!this.model.id){
            let rows = this.NoticeMaterialTable.getNewDataWithId()
            this.NoticeMaterialTable.removeRowsById(rows[0].id)
            let rows_car = this.CarTableRef.getNewDataWithId()
            this.CarTableRef.removeRowsById(rows_car[0].id)
            this.NoticeMaterialTable.syncHeaderScroll = true;
            this.CarTableRef.syncHeaderScroll = true;
          }
          this.scrollTop()
        },500)
      })
    },
    methods: {
      locationOk(data){
        this.$refs.details.updateData('erpWarehouseCode',data.warehouseCode)
        this.$refs.details.updateData('erpWarehouseName',data.warehouseName)
        this.$refs.details.updateData('erpLocationCode',data.code)
        this.$refs.details.updateData('erpLocationName',data.name)
      },
      onSelectLocation(){
        this.$refs.location.add();
      },
      deleteCarinfo(props){
        // this.bussStorageNoticeCarinfoTable.dataSource.
        const { row } = props
        const dataSource = [...this.bussStorageNoticeCarinfoTable.dataSource];
        this.bussStorageNoticeCarinfoTable.dataSource = dataSource.filter(item => item !== row);
      },
      handleEdit(props){
        this.noticeMaterialRowNum = props.index
        this.$refs.details.open(props,this.bussStorageNoticeMaterialTable.dataSource)
      },
      handleEditCar(props){
        this.$refs.carDetail.open(props,this.bussStorageNoticeCarinfoTable.dataSource)
      },
      handleDeleteMaterical(props){
        this.bussStorageNoticeMaterialTable.dataSource.splice(props.index,1)
        this.scrollTop()
        this.deleteCarByMat(props.row.materialCode);
      },
      handleCK(props){
        this.addCarInfo(props.row)
      },
      addCarInfo(row){
        let car = {}
        car.materialName = row.materialName
        car.materialCode = row.materialCode
        car.meUnit = row.meUnit
        this.bussStorageNoticeCarinfoTable.dataSource.push(car)
        this.scrollTop()
      },
      deleteCarByMat(code){
        // 删除车辆信息
        let data = this.bussStorageNoticeCarinfoTable.dataSource.filter(item=> item.materialCode !== code)
        this.bussStorageNoticeCarinfoTable.dataSource =  data
      },
      goodOk(rows){
        let _this = this;
        let jsonArray = rows.map(item => ({
          ...item,
          id: '',
          materialId: item.id,
          materialName: item.name,
          materialCode: item.code,
          supplierId: item.supplierId,
          spec: item.noneSpec,
          meUnit: item.meUnit,
          remark: ''
        }))
        let filteredJsonArray = jsonArray.filter(item=> {
          if(this.bussStorageNoticeMaterialTable.dataSource.length>0){
            const alice = this.bussStorageNoticeMaterialTable.dataSource.find(obj => item.materialCode === obj.materialCode && item.supplierId === obj.supplierId);
            return !alice;
          }
          return true;
        })
        // this.NoticeMaterialTable.pushRows(... filteredJsonArray)
        // this.tableRef.pushRows(jsonarray)
        this.bussStorageNoticeMaterialTable.dataSource.push(... filteredJsonArray)
        this.scrollTop()
      },
      addMaterial(){
        // 新增物料
        // 选择采购//getSelectionData
      },
      deleteMaterial(){
        // 删除物料
        let rows =  this.NoticeMaterialTable.selectedRows;
        if(rows.length<=0){
          this.$message.warn('请选择收货信息！')
          return;
        }
        console.log(rows)
        rows.forEach(row=>{
          this.bussStorageNoticeMaterialTable.dataSource = this.bussStorageNoticeMaterialTable.dataSource.filter(item=>item.id !== row.id)
          this.deleteCarByMat(row.materialCode);
        })

      },
      clearCheck(){
        this.NoticeMaterialTable.clearSelection();
      },
      showGoogs(){
        this.$nextTick(() => {
          this.$refs.good.add()
        })
      },
      onSearchSupplier(){
        this.$nextTick(() => {
          this.$refs.supplier.add();
        })
      },
      supplierOk(data){
        this.$set(this.model,'supplierName',data.name)
        this.$set(this.model,'supplierId',data.id)
      },
      procurementOk(data){
        getAction('/business/bussMaterialProcurement/queryById',{id: data.id }).then( response =>{
          const { result } = response ;
          const { detaillist } = result;
          let filteredJsonArray = detaillist.map(item => ({
              ...item,
              id: '',
              procurementDetailId: item.id
          }))
          this.bussStorageNoticeMaterialTable.dataSource = []
          // this.scrollTop()
          this.bussStorageNoticeMaterialTable.dataSource.push(... filteredJsonArray)
          // this.deleteIdProperty(this.bussStorageNoticeMaterialTable.dataSource);
          this.loadVendor(filteredJsonArray);

        })
      },
      loadVendor(filteredJsonArray){
        //加载供应商
        const procurment = filteredJsonArray[0];
        this.model.supplierId = procurment.supplierId;
        this.model.supplierName = procurment.supplierName
        this.model.procurementCode = procurment.code
        this.model.procurementId = procurment.procurementId
      },
      onSearchProcurement(){
        this.$nextTick(() => {
          this.$refs.procurement.add();
        })
      },
      handlerChanged(data){
        this.model.supplierId = ''
        this.model.procurementCode = ''
        this.model.supplierName = ''
        this.addBefore()
        // this.loaded()
      },
      addBefore(){
        this.bussStorageNoticeCarinfoTable.dataSource = []
        this.bussStorageNoticeMaterialTable.dataSource = []
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        this.$nextTick(() => {
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.bussStorageNoticeCarinfo.list, params, this.bussStorageNoticeCarinfoTable)
          this.requestSubTableData(this.url.bussStorageNoticeMaterial.list, params, this.bussStorageNoticeMaterialTable)
        }
        // this.loaded()
      },
      //校验所有一对一子表表单
      validateSubForm(allValues){
          return new Promise((resolve,reject)=>{
            Promise.all([
            ]).then(() => {
              resolve(allValues)
            }).catch(e => {
              if (e.error === VALIDATE_FAILED) {
                // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
                this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
              } else {
                console.error(e)
              }
            })
          })
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let _this = this;
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          bussStorageNoticeMaterialList: _this.processingData( allValues.tablesValue[0].tableData),
          bussStorageNoticeCarinfoList: _this.processingData(allValues.tablesValue[1].tableData),
        }
      },
      processingData(data){
        let newJsonArray = data.map(item => {
          return { ...item, id: item.id && item.id.indexOf('row')>=0?'':item.id  };
        });
        return newJsonArray;
      },
      validateError(msg){
        this.$message.error(msg)
      },
      scrollTop(){
        // 数据变动会出现滚动条无法滚动的情况，调用这个
        this.NoticeMaterialTable.resetScrollTop();
        // this.CarTableRef.resetScrollTop();
        // getRefPromise(this, 'bussStorageNoticeMaterial').then(vxeTable => {
        //   vxeTable.resetScrollTop()
        // })
      },
      handleDetailsConfirm({row,datasource,index, callback}) {
        // 校验当前行 app.$refs['formInline']
        if(datasource === this.bussStorageNoticeMaterialTable.dataSource){
          this.$refs.mainForm.validate(valid=>{
            this.detailsConfirm(valid,row,datasource,index,callback);
          })
        }else {
          this.$refs.subForm.validate(valid=>{
            this.detailsConfirm(valid,row,datasource,index,callback);
          })
        }
      },
      detailsConfirm(valid,row,datasource,index, callback){
        if (valid) {
          callback(true)
          Object.assign(datasource[index], row);
        } else {
          callback(false)
          this.$message.warn('校验失败')
        }
      }
    }
  }
</script>

<style scoped>
</style>